package com.example.ploy4j.controller;

import com.example.ploy4j.dto.GitRepositoryConfigDto;
import com.example.ploy4j.dto.ServerConfigDto;
import com.example.ploy4j.service.GitRepositoryConfigService;
import com.example.ploy4j.service.ServerConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Collections;
import java.util.List;

@Controller
@RequiredArgsConstructor
public class HomeController {

    private final ServerConfigService serverConfigService;
    private final GitRepositoryConfigService gitRepositoryConfigService;

    @GetMapping("/")
    public String homePage(Model model) {
        boolean hasServerConfigs = !serverConfigService.getAllServerConfigs().isEmpty();
        boolean hasGitRepoConfigs = !gitRepositoryConfigService.getAllGitRepositoryConfigs().isEmpty();
        model.addAttribute("hasServerConfigs", hasServerConfigs);
        model.addAttribute("hasGitRepoConfigs", hasGitRepoConfigs);

        if (hasGitRepoConfigs) {
            List<GitRepositoryConfigDto> projects = gitRepositoryConfigService.getAllGitRepositoryConfigs();
            model.addAttribute("projects", projects);
        } else {
            model.addAttribute("projects", Collections.emptyList());
        }

        return "index";
    }
} 